package com.printeverywhere.job;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.printeverywhere.delegator.CommonFunctionDelegator;
import com.printeverywhere.dto.BannerCampaigns;
import com.printeverywhere.dto.BannerHourlyStatistics;
import com.printeverywhere.util.AdminConstants;
import com.printeverywhere.util.DateUtil;
import com.printeverywhere.util.SearchCriteria;

public class ProcessBannerHourlyStatisticsJob implements Job, ServletRequestAware{
	private static Logger logger = Logger.getLogger(ProcessEndedAuctionsJob.class);

	private HttpServletRequest request;

	private List<Criterion> criterionList = new ArrayList<Criterion>();

	private List<BannerCampaigns> bannerCampaignsList = new ArrayList<BannerCampaigns>();

	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		try {
			if(criterionList.size() > 0){
				criterionList.clear();
			}
			criterionList.add(Restrictions.ge("endDate", DateUtil.getTodaysDate()));
			criterionList.add(Restrictions.leProperty("clicks", "maxClicks"));
			criterionList.add(Restrictions.leProperty("days", "maxDays"));
			criterionList.add(Restrictions.leProperty("views", "maxViews"));
			bannerCampaignsList = (List<BannerCampaigns>) CommonFunctionDelegator.getObjectsByCriterion(criterionList, null, null, BannerCampaigns.class);
			for (Iterator<BannerCampaigns> iterator = bannerCampaignsList.iterator(); iterator.hasNext();) {
				BannerCampaigns bannerCampaigns = (BannerCampaigns) iterator.next();
				BannerHourlyStatistics bannerHourlyStatistics = new BannerHourlyStatistics();
				BannerHourlyStatistics bannerHourlyStatisticsTemp = new BannerHourlyStatistics();
				if(criterionList.size() > 0){
					criterionList.clear();
				}
				criterionList.add(Restrictions.eq("bannerCampaigns", bannerCampaigns));
				List<BannerHourlyStatistics> bannerHourlyStatisticsList = (List<BannerHourlyStatistics>) CommonFunctionDelegator.getObjectsByCriterion(criterionList, Order.desc("statisticsId"), null, BannerHourlyStatistics.class);
				if(bannerHourlyStatisticsList.size() > 0){
					bannerHourlyStatisticsTemp = bannerHourlyStatisticsList.get(AdminConstants.ZERO_NUMBER);
				}
				bannerHourlyStatistics.setBannerCampaigns(bannerCampaigns);
				if(bannerHourlyStatisticsTemp.getClicks()> 0){
					bannerHourlyStatistics.setClicks(bannerCampaigns.getClicks() - bannerHourlyStatisticsTemp.getClicks());
				}else{
					bannerHourlyStatistics.setClicks(bannerCampaigns.getClicks());
				}
				if(bannerHourlyStatisticsTemp.getImpression()> 0){
					bannerHourlyStatistics.setImpression(bannerCampaigns.getViews() - bannerHourlyStatisticsTemp.getImpression());
				}else{
					bannerHourlyStatistics.setImpression(bannerCampaigns.getViews());
				}
				bannerHourlyStatistics.setDate(DateUtil.getTodaysDate());
				bannerHourlyStatistics.setHour(DateUtil.getTodaysDate());

				CommonFunctionDelegator.generalInsertbyobj(bannerHourlyStatistics);
			}
		} catch (Exception e) {
			logger.error("Error in generating banner hourly statistics.");
		}
	}

	public void setServletRequest(HttpServletRequest arg0) {
		request = arg0;
	}


}
